//版本三
//过程抽象
const traffic = document.getElementById('traffic');

function wait(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
}

function poll(...fnList) {
    let stateIndex = 0;
    return async function (...args) {
        let fn = fnList[stateIndex++ % fnList.length];
        return await fn.apply(this, args);
    }
}


async function setState(state, ms) {
    traffic.className = state;
    await wait(ms);
}

let trafficStatePull = poll(setState.bind(null, 's1', 500),
                            setState.bind(null, 's2', 500),
                            setState.bind(null, 's3', 500));


(async function () {
     //noprotect
    while (1){
        await trafficStatePull();
    }
}());